import java.io.*;
import java.net.*;

class requestVMs
{
 	public static void main(String args[]) throws Exception
 	{
		int pid=5000;
		if(args.length>0) {
			try {
				pid = Integer.parseInt(args[0]);
			} catch (NumberFormatException e) {
				System.err.println("Argument" + " must be an integer");
				System.exit(1);
			}
		}
		
		try {

		  	String answer;
			Socket clientSocket;
			ObjectOutputStream outToServer;
			ObjectInputStream inFromServer;
			BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));

			clientSocket = new Socket("localhost", pid);
		  	outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
		  	inFromServer = new ObjectInputStream(clientSocket.getInputStream());
			boolean connected=true;

		  	outToServer.writeObject(new String("Hello"));
      		answer = (String) inFromServer.readObject(); 
		  	printStream(answer);
	
			while (connected) {
				System.out.println("Your options are: \n"+
                                                   "\tlistVMs to see a list avaliable VMtypes \n"+
						   "\trequestVMs to submit your VM request \n"+
						   "\tbye to exit");

		  		String userRequest=inFromUser.readLine();

				if(userRequest.equals("listVMs")) {
					//list VM types available
		  			outToServer.writeObject(new String("ListVms"));
					answer = (String) inFromServer.readObject(); 
	  				printStream(answer);
		  			
				} else if(userRequest.equals("requestVMs")) {    			
				  	//get client request for vms
					int [] vmRequest= new int[5];
				  	System.out.println( "Please specify VM Request: " );
				  	System.out.println( "Number of tiny VMs needed" );
					vmRequest[0]=Integer.parseInt(inFromUser.readLine()); 
				  	System.out.println( "Number of small VMs needed" );
					vmRequest[1]=Integer.parseInt(inFromUser.readLine()); 
				  	System.out.println( "Number of medium VMs needed" );
					vmRequest[2]=Integer.parseInt(inFromUser.readLine()); 
				  	System.out.println( "Number of large VMs needed" );
					vmRequest[3]=Integer.parseInt(inFromUser.readLine()); 
				  	System.out.println( "Number of huge VMs needed" );
					vmRequest[4]=Integer.parseInt(inFromUser.readLine()); 
					
		  			outToServer.writeObject(new String("RequestVms"));
				  	outToServer.writeObject(vmRequest);
					answer = (String) inFromServer.readObject(); 
	  				printStream(answer);
	  				
	  				System.out.println("Do you want to purchase the vms (y/n)");
			  		String userResponse=inFromUser.readLine();
			  		if(userResponse.equals("y")) {
			  			outToServer.writeObject(new String("PurchaseVms"));
						answer = (String) inFromServer.readObject(); 
		  				printStream(answer);
			  		} else {
			  			outToServer.writeObject(new String("CancelVms"));
						answer = (String) inFromServer.readObject(); 
		  				printStream(answer);			  			
			  		}

				} else if(userRequest.equals("bye")) {    			
					connected=false;
		  			outToServer.writeObject(new String("Goodbye"));
					answer = (String) inFromServer.readObject(); 
	  				printStream(answer);			  			
		  			
				}
			}
			//close socket
			outToServer.close();
			inFromServer.close();
		  	clientSocket.close();		

		} catch (ConnectException e) {
	        	System.err.println("pid" + " must refer to a valid socket");
	        	System.exit(1);
		} catch (ClassNotFoundException e) {
        	System.err.println("Object class incompatable"+e);
        	System.exit(1);
	        	
		}				

 	}
 	
 	public static void printStream(String answer)
 	{
 		answer=answer.replaceAll("#", "\n");
 		System.out.println(answer);

 	}
}
